package com.usooft.metaTalent.core.platform.service.base;

import com.usooft.framework.core.query.Limit;
import com.usooft.framework.core.query.Page;
import com.usooft.metaTalent.core.platform.entity.Video;
import com.usooft.metaTalent.core.platform.mapper.VideoMapper;
import com.usooft.metaTalent.core.platform.query.VideoQuery;
import com.usooft.metaTalent.core.platform.service.VideoService;
import com.usooft.metaTalent.core.platform.wrapper.VideoWrapper;
import java.lang.Long;
import java.lang.Override;
import java.lang.String;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.lang.NonNull;

public abstract class BaseVideoService implements VideoService {
    @Autowired
    private VideoMapper videoMapper;

    @Override
    public Video get(@NonNull Long id) {
        return find(id).orElseThrow();
    }

    @Override
    public Optional<Video> find(@NonNull Long id) {
        return Optional.ofNullable(videoMapper.get(id));
    }

    @Override
    public List<VideoWrapper> list(VideoQuery query) {
        return videoMapper.list(query);
    }

    @Override
    public Page<VideoWrapper> page(VideoQuery query) {
        int rows = videoMapper.count(query.getFilter());
        return new Page<>(rows, rows == 0 ? new ArrayList<>() : videoMapper.list(query));
    }

    protected Optional<Video> findOne(VideoQuery.Filter filter, VideoQuery.Sorts sorts) {
        List<VideoWrapper> wrappers = list(VideoQuery.builder()
                                .filter(filter)
                                .sorts(sorts)
                                .limit(Limit.rowCount(1))
                                .build());
        return wrappers.stream().findFirst().map(Function.identity());
    }

    protected Optional<Video> findOne(VideoQuery.Filter filter) {
        return findOne(filter, VideoQuery.Sorts.builder().build());
    }

    @Override
    public int count(VideoQuery.Filter filter) {
        return videoMapper.count(filter);
    }

    protected Video internalAdd(String name, String videoUrl, String suffix, Long articleId) {
        Video video = new Video();
        video.setName(name);
        video.setVideoUrl(videoUrl);
        video.setSuffix(suffix);
        video.setArticleId(articleId);

        video.setCreatedDate(LocalDateTime.now());
        video.setVersion(0);
        video.setIsDelete(false);
        videoMapper.add(video);
        return video;
    }

    protected void internalUpdate(@NonNull Video video) {
        video.setLastModifiedDate(LocalDateTime.now());

        int rows = videoMapper.update(video);
        if (rows == 0) {
            throw new OptimisticLockingFailureException("更新失败。可能视频 (" + video.getId() + ") 的版本 (" + video.getVersion() + ") 已不是最新");
        }
        video.setVersion(video.getVersion() + 1);
    }

    protected void internalDelete(@NonNull Video video) {
        int rows = videoMapper.delete(video);
        if (rows == 0) {
            throw new OptimisticLockingFailureException("删除失败。可能视频 (" + video.getId() + ") 的版本 (" + video.getVersion() + ") 已不是最新");
        }
    }
}
